package action;

import global.Constants;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.AccountBookService;
import util.ServletHelp;
import util.StringUtils;
import domain.AccountBook;

/**
 * 账本Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class AccountBookAction extends BaseAction {
	/**
	 * 账本Service
	 * */
	private AccountBookService accountBookService = null;

	/**
	 * 设定账本Service
	 * @param accountBookService 账本Service
	 */
	public void setAccountBookService(AccountBookService accountBookService) {
		this.accountBookService = accountBookService;
	}
	
	/**
	 * 收支项目查询方法
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 参数
		AccountBook paramAccountBook = new AccountBook();
		// 取得收支项目列表
		List<AccountBook> accountBookList = new ArrayList<AccountBook>();
		JSONArray datas = new JSONArray();
		accountBookList = accountBookService.selectByCriteria(paramAccountBook);
		if(null!=accountBookList){
			datas = JSONArray.fromObject(accountBookList);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("datas", datas);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	/**
	 * 获得账本树
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward getAccountBookTree(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// 取得账本列表
		List<AccountBook> accountBookList = new ArrayList<AccountBook>();
		AccountBook paramAccountBook = new AccountBook();
		accountBookList = accountBookService.selectByCriteria(paramAccountBook);
		JSONArray res = getAccountBookTreeFromList(accountBookList);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 保存账本
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得参数
		String accountBookName = request.getParameter("accountBookName");
		String accountBookId = request.getParameter("accountBookId");
		// 设定参数
		AccountBook accountBook = new AccountBook();
		accountBook.setStatus(Constants.STATUS_1);
		accountBook.setDelflag(Constants.DEL_FLAG_1);
		if(StringUtils.isNotEmpty(accountBookName)){
			if(StringUtils.isNotEmpty(accountBookId)){
				accountBook.setAccountBookId(Integer.parseInt(accountBookId));
				accountBook.setAccountBookName(accountBookName);
				// 更新账本
				accountBookService.update(accountBook);
			}else{
				accountBook.setAccountBookName(accountBookName);
				// 添加账本
				accountBookService.insert(accountBook);
			}
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 删除账本
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数账本ID
		String accountBookId = request.getParameter("accountBookId");
		if (StringUtils.isNotEmpty(accountBookId)) {
			// 删除账本
			accountBookService.delete(Integer.parseInt(accountBookId));
		}
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 返回账目树
	 * @param accountBookList
	 * @return
	 */
	private JSONArray getAccountBookTreeFromList(List<AccountBook> accountBookList) {
		JSONArray results = new JSONArray();
		// 所有账目
		JSONObject allAccount = new JSONObject();
		allAccount.put("id", "allAccount");
		allAccount.put("text", "所有账目");
		allAccount.put("leaf", true);
		results.add(allAccount);
		// 账本
		if (null != accountBookList) {
			JSONArray children = new JSONArray();
			AccountBook temp = new AccountBook();
			for (int i = 0; i < accountBookList.size(); i++) {
				temp = accountBookList.get(i);
				JSONObject accountBookNode = new JSONObject();
				accountBookNode.put("id", temp.getAccountBookId());
				accountBookNode.put("text", temp.getAccountBookName());
				accountBookNode.put("leaf", true);
				children.add(accountBookNode);
			}
			JSONObject accountBook = new JSONObject();
			accountBook.put("id", "0");
			accountBook.put("text", "我的账本");
			accountBook.put("children", children);
			results.add(accountBook);
		}
		// 收支项目
		JSONObject accountBookOpt = new JSONObject();
		accountBookOpt.put("id", "accountCategory");
		accountBookOpt.put("text", "收支项目");
		accountBookOpt.put("leaf", true);
		results.add(accountBookOpt);

		return results;
	}
}
